package offer.offer03;

import java.util.Arrays;

/**
 * 100, 30先二分法查找然后左右看看?
 */
public class Solution53 {
    public int search(int[] nums, int target) {
        int posi = Arrays.binarySearch(nums, target);
        if(posi < 0) return 0;
        //这个posi是二分法第一个找到的位置, 他的左右都有可能是他, 这是个排序数组
        int startLoc = posi;
        int endLoc = posi;
        while (nums[startLoc] == target){
            startLoc --;
            if(startLoc == -1) break;
        }
        while (nums[endLoc] == target){
            endLoc ++;
            if(endLoc == nums.length) break;
        }
        return endLoc - startLoc - 1;
    }

    public static void main(String[] args) {
        int[] input = new int[]{1, 2, 3, 4, 5, 5, 6, 6, 6, 6, 7, 9, 9};
        //int[] input = new int[]{};
        System.out.println(new Solution53().search(input, 1));
        System.out.println(new Solution53().search(input, 5));
    }
}
